<template>
  <div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="102px"
      @submit.native.prevent
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系电话"
          clearable
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入证件号"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参展年限" prop="joinYear">
        <el-select
          style="width: 160px"
          v-model="queryParams.joinYear"
          placeholder="请选择参展年限"
          @change="selectYear"
        >
          <el-option
            v-for="item in dict.type.sys_exhibit_year"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请证件类型" prop="applyCardType">
        <el-select
          style="width: 170px"
          v-model="queryParams.applyCardType"
          placeholder="请选择申请证件类型"
          class="w100"
          filterable
          clearable
          @change="selectApply"
        >
          <el-option
            v-for="item in dict.type.sys_apply_one"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          style="width: 160px"
          v-model="queryParams.status"
          placeholder="审核状态"
          clearable
          @change="selectStatus"
        >
          <el-option
            v-for="dict in dict.type.sys_apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          />
          <el-button
            v-if="scope.row.status != 1 && scope.row.status != 3"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
          />
          <el-button
            v-if="scope.row.status != 1 && scope.row.status != 3"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="展区名称"
        align="center"
        prop="deptName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="身份证号"
        align="center"
        prop="idCard"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单位"
        align="center"
        prop="comName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="mobile"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="报名时间"
        align="center"
        prop="createTime"
        width="165"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
        width="90"
      >
        <template slot="header" slot-scope="scope">
          审核状态
          <el-tooltip
            content="审核通过后，如发现信息填报错误请联系上级单位对信息进行删除，重新上传信息。重新上传的信息需要二次审核"
            placement="top"
          >
            <i class="el-icon-question cp"></i>
          </el-tooltip>
        </template>

        <template slot-scope="scope">
          <span v-if="scope.row.status === 0" class="gray">待审核</span>
          <span v-if="scope.row.status === 1" class="green">市级通过</span>
          <span v-if="scope.row.status === 2" class="red">市级驳回</span>
          <span v-if="scope.row.status === 3" class="green">省级通过</span>
          <span v-if="scope.row.status === 4" class="red">省级驳回</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核意见"
        align="center"
        prop="auditInfo"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0"> -</span>
          <span v-if="scope.row.status === 1 || scope.row.status === 2">
            {{ scope.row.cityAuditInfo || "-" }}
          </span>
          <span v-if="scope.row.status === 3 || scope.row.status === 4">
            {{ scope.row.auditInfo || "-" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        align="center"
        prop="auditBy"
        width="90"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0"> -</span>
          <span v-if="scope.row.status === 1 || scope.row.status === 2">
            {{ scope.row.cityAudit || "-" }}
          </span>
          <span v-if="scope.row.status === 3 || scope.row.status === 4">
            {{ scope.row.auditBy || "-" }}
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      class="auto-dialog"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        style="margin-top: -30px"
      >
        <el-divider content-position="left">
          <div class="m-title">填报信息：</div>
        </el-divider>
        <el-form-item label="选择展区：" prop="deptId">
          <treeselect
            v-model="form.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择展区"
            :flat="true"
            :normalizer="myNormalizer"
            class="no_tree"
          />
        </el-form-item>
        <el-form-item label="姓名：" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别：" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" class="w100">
            <el-option
              v-for="item in dict.type.sys_user_sex"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="国籍：" prop="country">
          <el-select
            v-model="form.country"
            placeholder="请选择国籍"
            filterable
            clearable
            style="width: calc(100% - 20px); margin-right: 6px"
          >
            <el-option
              v-for="item in dict.type.sys_user_country"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-tooltip content="说明：支持搜索" placement="top">
            <span> <i class="el-icon-question"></i> </span>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="证件类型：" prop="cardType">
          <el-select
            v-model="form.cardType"
            placeholder="请选择证件类型"
            class="w100"
            filterable
            clearable
          >
            <el-option
              v-for="item in dict.type.sys_card_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号：" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入证件号" />
        </el-form-item>
        <el-form-item label="户籍地址：" prop="address">
          <el-input
            v-model="form.address"
            placeholder="请输入户籍地址"
            style="width: calc(100% - 20px); margin-right: 6px"
          />
          <el-tooltip content="户籍地址与身份证地址填写一致" placement="top">
            <span> <i class="el-icon-question"></i> </span>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="证件类型：" prop="cardType">
          <el-select
            v-model="form.cardType"
            placeholder="请选择证件类型"
            class="w100"
            filterable
            clearable
          >
            <el-option
              v-for="item in dict.type.sys_card_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="证件号：" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入证件号" />
        </el-form-item>
        <el-form-item label="单位：" prop="comName">
          <el-input v-model="form.comName" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="职务：" prop="post">
          <el-input v-model="form.post" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="电话：" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="申请证件类型：" prop="applyCardType">
          <el-select
            v-model="form.applyCardType"
            placeholder="请选择申请证件类型"
            class="w100"
            filterable
            clearable
          >
            <el-option
              v-for="item in dict.type.sys_apply_one"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签证地：" prop="placeArea">
          <el-input v-model="form.placeArea" placeholder="请输入签证地" />
        </el-form-item>
        <el-form-item label="人员照片：" prop="avgUrl" ref="imgViod">
          <div>
            <div
              v-if="form.avgUrl"
              style="width: 100px; height: 100px; position: relative"
            >
              <div v-if="form.avgUrl" class="demo-image__preview w100-img">
                <el-image
                  :src="form.avgUrl"
                  :preview-src-list="[form.avgUrl]"
                  class="avatar"
                >
                </el-image>
              </div>
              <i
                v-if="form.avgUrl"
                class="el-icon-delete remove-img"
                @click="handleRemove"
              />
            </div>
            <uploadBig
              v-else
              :showBtn="true"
              btnText="上传图片"
              upWidth="50px"
              upHeight="30px"
              upLineHeight="30px"
              :accept="uploadAccept"
              :showFileList="false"
              :fileSize="5"
              @getUpload="uploadAvgUrl"
            />
          </div>
          <div class="w100 small-text">
            提示：建议上传白底一寸免冠照，照片尺寸为
            <span class="red">295*413</span> ， ，图片大小不超过5M，格式为
            <span class="red">{{ uploadAccept }}</span>
            的文件
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="btnLoading">
          确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情— -->
    <el-dialog
      title="查看填报信息"
      :visible.sync="detailOpen"
      width="800px"
      append-to-body
      class="auto-dialog"
    >
      <detailDialog ref="detailInfo" :detailForm="detailForm" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  applyList,
  applyInfo,
  editApply,
  removeApply,
} from "@/api/project/paper";
import { deptTreeSelect } from "@/api/system/user";

import detailDialog from "@/components/Project/dialog/detail/paper";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "User",
  dicts: [
    "sys_apply_status",
    "sys_user_sex",
    "sys_exhibit_year",
    "sys_apply_one",
    "sys_card_type",
    "sys_user_country",
  ],
  components: { Treeselect, detailDialog },
  data() {
    const checkNum = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error("请输入证件号"));
      } else {
        if (this.form.cardType === "居民身份证") {
          if (value.toString().length != 18) {
            callback(new Error("限制可输入18位，请重新输入"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    };
    return {
      // 遮罩层
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        status: undefined,
        showArea: undefined,
        status: undefined,
        joinYear: "2025",
        idCard: undefined,
        applyCardType: undefined,
        sig: 0,
        userType: 0,
        placeArea: undefined,
      },

      // 总条数
      total: 0,
      list: [],

      // 弹出层标题
      title: "",
      open: false,
      form: {},
      btnLoading: false,

      form: {},
      // 表单校验
      rules: {
        deptId: [{ required: true, message: "请选择展区", trigger: "change" }],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        idCard: [{ required: true, validator: checkNum, trigger: "blur" }],
        address: [
          { required: true, message: "请输入户籍地址", trigger: "blur" },
        ],
        comp: [{ required: true, message: "请选择国籍", trigger: "change" }],
        comName: [
          { required: true, message: "请输入单位名称", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择证件类型", trigger: "change" },
        ],
        paper: [{ required: true, message: "请输入证件号", trigger: "blur" }],
        post: [{ required: true, message: "请输入职务", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }],
        avgUrl: [
          { required: true, message: "请上传人员照片", trigger: "blur" },
        ],
        country: [{ required: true, message: "请选择国籍", trigger: "change" }],
        cardType: [
          { required: true, message: "请选择证件类型", trigger: "change" },
        ],
        applyCardType: [
          { required: true, message: "请选择申请证件类型", trigger: "change" },
        ],
      },

      detailForm: {},
      detailOpen: false,

      uploadAccept: ".png, .jpg, .jpeg",

      // 部门树选项
      deptOptions: [],
    };
  },

  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      applyList(this.queryParams)
        .then((response) => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect({ flag: this.queryParams.sig }).then((response) => {
        this.deptOptions = response.data;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partId: undefined,
        deptId: undefined,
        name: undefined,
        sex: undefined,
        idCard: undefined,
        address: undefined,
        comName: undefined,
        post: undefined,
        mobile: undefined,
        avgUrl: undefined,
        showArea: undefined,
        showNum: undefined,
        showPart: undefined,
        userType: this.queryParams.userType,

        country: undefined,
        cardType: undefined,
        applyCardType: undefined,
        sig: this.queryParams.sig,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      applyInfo(row.partId).then((response) => {
        this.form = response.data;
        this.form.sig = this.queryParams.sig;
        this.open = true;
        this.title = "修改申请信息";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          if (this.form.partId != undefined) {
            editApply(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.btnLoading = false;
              });
          } else {
            addApply(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.btnLoading = false;
              });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const partId = row.partId;
      this.$modal
        .confirm("是否确认删除选中的数据项？")
        .then(function () {
          return removeApply(partId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    // 图片上传
    uploadAvgUrl(data) {
      this.form.avgUrl = data;
      this.$refs.imgViod.clearValidate();
    },

    // 移除封面图
    handleRemove() {
      this.form.avgUrl = undefined;
    },

    // 选择状态
    selectStatus() {
      this.getList();
    },

    // 查看详情
    handleView(row) {
      applyInfo(row.partId).then((res) => {
        this.detailForm = res.data;
        this.detailOpen = true;
      });
    },

    selectYear() {
      this.getList();
    },

    selectApply() {
      this.getList();
    },

    myNormalizer(node) {
      return {
        id: node.id,
        label: node.label,
        parentId: node.parentId,
        isDisabled: Number(node.parentId) < 101,
        children: node.children,
      };
    },
  },
};
</script>
